package com.yesep.client;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.EventLoop;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @program: gateway
 * @description: netty处理器
 * @author: quliang
 * @create: 2019-07-09 13:44
 **/
@ChannelHandler.Sharable
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

//    static final Logger logger = Logger.getLogger(NettyClientHandler.class);

    NettyClient nettyClient;

    private String heartbeat;

    public NettyClientHandler(NettyClient nettyClient) {
        this.nettyClient = nettyClient;
    }

    public NettyClientHandler() {
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        // 下行的数据
//        String json = String.valueOf(msg);
//        Data data = (Data) JsonUtils.JSONToObject(json, Data.class);
//        this.nettySocketClient.getRespondDataThread().put(data);

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        final EventLoop eventLoop = ctx.channel().eventLoop();
        final NettyClient client = this.nettyClient;
        /*eventLoop.schedule(new Runnable() {
            @Override
            public void run() {
                client.connect(client.configureBootstrap(new Bootstrap(),
                        eventLoop));
            }

        }, client.getReConnInterval(), TimeUnit.SECONDS);*/
        super.channelInactive(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
        // logger.info("channelReadComplete--->");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
            throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            switch (e.state()) {
                case WRITER_IDLE:
                    // 把心跳发到上报的线程中
//                    NettyClientBootstrap.getClient().getReportDataThread()
//                            .put(data);
                    break;
                case READER_IDLE:

                    break;
                case ALL_IDLE:

                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx)
            throws Exception {
        super.channelWritabilityChanged(ctx);
    }
}

